package com.sdx.console.uploadcloud.web;

import com.alibaba.fastjson.JSONObject;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.sdx.common.utils.FileUtils;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.console.base.entity.BaseUniv;
import com.sdx.service.ConfigService;
import com.sdx.third.AliOSSApi;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UploadCloudController
 * @Description:
 * @author: kangbin
 * @date: 2019/4/17 14:53
 */
@RequestMapping("/uploadCloud")
@Controller
public class UploadCloudController extends AuthorizedBaesController {
    private static final Logger log = LoggerFactory.getLogger(UploadCloudController.class);

    @Resource
    private SQLManager sqlManager;

    private final String SQL = "uploadCloud.uploadCloud";

    @RequestMapping("/uploadLogo.json")
    @ResponseBody
    public String uploadLogo() {
        List<BaseUniv> list = new ArrayList<>();
        list = sqlManager.select(SQL+".selectBaseUnivLogoList",BaseUniv.class,null);
        int count = 0;
        for(BaseUniv baseUniv:list){
            Map<String,Object> map = new HashMap<>();
            map.put("univId",baseUniv.getUnivId());
            String logoCloud ="";
            String backGroundCloud="";
            if(StringUtils.isNotBlank(baseUniv.getLogo())&&!baseUniv.getLogo().toLowerCase().startsWith("http")){
                logoCloud = uploadCloud(baseUniv.getLogo());
                map.put("logoCloud",logoCloud);
            }

            if(StringUtils.isNotBlank(baseUniv.getBackGround())&&!baseUniv.getBackGround().toLowerCase().startsWith("http")){
                backGroundCloud =  uploadCloud(baseUniv.getBackGround());
                map.put("backGroundCloud",backGroundCloud);
            }
            if(!(StringUtils.isBlank(logoCloud)&&StringUtils.isBlank(backGroundCloud))){
                sqlManager.update(SQL+".updateBaseUniv",map);
                count++;
            }
        }
        return  "total:"+list.size()+"-->success:"+count;

    }

    public String uploadCloud(String storeFile) {
        try {
            String bucketName = "sdxcomm";
            AliOSSApi.uploadLocalFile(FileUtils.getRoot()+storeFile.substring(7),bucketName,storeFile.substring(1));
            String url = "https://sdxcomm."+ ConfigService.ossDomain+storeFile;
            return url;
        }catch (Exception e){

        }
        return null;
    }

    @RequestMapping("/checkCloudImg.json")
    @ResponseBody
    public JSONObject checkCloudImg() {
        List<BaseUniv> list = new ArrayList<>();
        list = sqlManager.select(SQL+".selectCloudImgList",BaseUniv.class,null);
        List<Object> objects = new ArrayList<>();
        for(BaseUniv baseUniv:list){
            int id = baseUniv.getUnivId();
             String univName = baseUniv.getUnivName();
            StringBuffer sb = new StringBuffer();
            if(StringUtils.isNotBlank(baseUniv.getLogoCloud())){
               if(!isImagesTrue(baseUniv.getLogoCloud())){
                   sb.append(univName+"(").append(id).append(") logo is 404   ");
               }
            }
            if(StringUtils.isNotBlank(baseUniv.getBackGroundCloud())){
                if(!isImagesTrue(baseUniv.getBackGroundCloud())){
                    sb.append(univName+"(").append(id).append(") backGround is 404  ");
                }
            }
            if(StringUtils.isNotBlank(sb.toString())){
                objects.add(sb);
            }
        }
        JSONObject json = new JSONObject();
        json.put("result","is ok!!!");
        if(objects.size()>0){
            json.put("result",objects);
        }
        return  json;

    }



    public boolean isImagesTrue(String posturl) {
        try {
            URL url = new URL(posturl);
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();

            if (urlcon.getResponseCode() == HttpURLConnection.HTTP_OK) {
                log.info(posturl+"  is  200");
                return  true;
            }else{
                log.info(posturl+"  is  404");
            }
        }catch(Exception e){

        }
        return false;
    }

}
